package cn.bctools.mail.api.util;

import cn.hutool.core.bean.BeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BeanCopyUtil {
    private static final Logger log = LoggerFactory.getLogger(BeanCopyUtil.class);

    public BeanCopyUtil() {
    }

    public static <T> List<T> copys(List source, Class<T> cls) {
        List<T> arrayList = new ArrayList();
        Iterator var3 = source.iterator();

        while(var3.hasNext()) {
            Object t = var3.next();

            try {
                T o = cls.newInstance();
                BeanUtil.copyProperties(t, o, new String[0]);
                arrayList.add(o);
            } catch (InstantiationException var6) {
                log.info("转化错误", var6);
            } catch (IllegalAccessException var7) {
                log.info("转化错误", var7);
            }
        }

        return arrayList;
    }

    public static <T> T copy(Object source, Class<T> cls) {
        T o = null;

        try {
            if (source instanceof List) {
                return (T) copys((List)source, cls);
            }

            o = cls.newInstance();
            BeanUtil.copyProperties(source, o, new String[0]);
            return o;
        } catch (InstantiationException var4) {
            log.info("转化错误", var4);
        } catch (IllegalAccessException var5) {
            log.info("转化错误", var5);
        }

        return o;
    }

    public static <T> T copy(Class<T> cls, Object... source) {
        T o = null;

        try {
            o = cls.newInstance();
            Object[] var3 = source;
            int var4 = source.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Object o1 = var3[var5];
                BeanUtil.copyProperties(o1, o, new String[0]);
            }

            return o;
        } catch (InstantiationException var7) {
            log.info("转化错误", var7);
        } catch (IllegalAccessException var8) {
            log.info("转化错误", var8);
        }

        return o;
    }

    public static Object copy(Object source, Object target) {
        BeanUtil.copyProperties(source, target, new String[0]);
        return target;
    }
}
